import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
#
#
# acc = np.load('../save/result_save/s7/acc.npy', allow_pickle=True)  # avg
# acc1 = np.load('../save/result_save/s8/acc.npy', allow_pickle=True)  # fml
# # acc2 = np.load('../save/result_save/s3/acc.npy', allow_pickle=True)  # fml
# print(acc[-1])
# # print(acc1[-1])
# plt.figure()
# # plt.subplot(2, 1, 1)
# plt.title('Loss of Global Model over Testset ')
# plt.plot(range(len(acc)), acc, 'r')
# plt.plot(range(len(acc)), acc1, 'g')
# # plt.plot(range(len(acc)), acc2, 'k')
# # plt.xlabel('communication rounds')
# plt.ylabel('Test loss')
# plt.legend(['s7', "s8",'s3'])
# #
# # plt.subplot(2, 1, 2)
# # plt.plot(range(len(acc3)), acc3, 'r')
# # plt.plot(range(len(acc2)), acc2, 'g', linestyle=':')
# #
# # plt.ylabel('Test loss')
# # plt.xlabel('Communication Rounds')
# #
# #
# # plt.legend(['FML,Non-IID', 'FedAvg,Non-IID'])
# plt.tight_layout()
# plt.savefig('./1.pdf')
#
#
# from torch.utils.tensorboard import SummaryWriter
# writer = SummaryWriter()
# r = 5
# for i in range(len(acc)):
#     writer.add_scalars('run_14h', {'s7':acc[i],
#                                     's8':acc1[i]}, i)
# writer.close()
# print("绘图完毕")
#
# import json

# with open("/home/shentao/zj/mycode/727FML/s7.json", 'r') as f:
#     s7 = json.loads(f.read())
# with open("/home/shentao/zj/mycode/727FML/s8.json", 'r') as f:
#     s8 = json.loads(f.read())
# ss7 = []
# ss8 = []
# for i in range(len(s7)):
#     s7i = s7[i]
#     ss7.append(s7i[2])
#     s8i = s8[i]
#     ss8.append(s8i[2])
# acc = np.array(ss7)
# acc1 = np.array(ss8)
# plt.title('Loss of Global Model over Testset ')
# plt.plot(range(len(acc)), acc, 'r')
# plt.plot(range(len(acc)), acc1, 'g')
# plt.savefig('./1.pdf')
# print("绘图完毕")


import pandas as pd
import numpy as np
import os


def smooth(data, weight=0.5):
    scalar = data
    last = scalar[0]
    smoothed = []
    for point in scalar:
        smoothed_val = last * weight + (1 - weight) * point
        smoothed.append(smoothed_val)
        last = smoothed_val

    return np.array(smoothed)
    # save = pd.DataFrame({'Step':data['Step'].values,'Value':smoothed})
    # save.to_csv('smooth_'+csv_path)


if __name__ == '__main__':
    x = ["a10","a11","a12"]
    acc = np.load('../save/result_save/{}/acc.npy'.format(x[0]), allow_pickle=True)  # avg
    acc1 = np.load('../save/result_save/{}/acc.npy'.format(x[1]), allow_pickle=True)  # avg
    acc2 = np.load('../save/result_save/{}/acc.npy'.format(x[2]), allow_pickle=True)  # fml
    # acc = smooth(acc)
    # acc1 = smooth(acc1)
    # acc2 = smooth(acc2)
    plt.title('Accuracy curve')
    plt.grid(ls='--')
    plt.plot(range(len(acc)), acc, 'b', linestyle=':', label="fedavg")
    plt.plot(range(len(acc1)), acc1, 'g',label="fedprox")
    plt.plot(range(len(acc2)), acc2, 'r',label="FML")
    # plt.legend(['e7', 'e8','e9'])
    plt.legend()
    plt.ylabel('Test accuracy')
    plt.xlabel('Communication rounds')
    # plt.savefig('./{}.png'.format(x[0]+x[1]+x[2]))
    plt.savefig("t.png")
    print("绘图完毕")